import java.util.Stack;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class SingleLinkedList {
    public HeroNode headNode=new HeroNode(0,"","");

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //System.out.println("链表有效结点个数:"+singleLinkedList.nodeCount());
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode heroNode5 = new HeroNode(5, "鲁智深", "花和尚");
        singleLinkedList.addByOrder(heroNode4);
        //singleLinkedList.addByOrder(heroNode4);
        singleLinkedList.addByOrder(heroNode1);
        //singleLinkedList.addByOrder(heroNode1);
        singleLinkedList.addByOrder(heroNode2);
        //System.out.println("链表有效结点个数:"+singleLinkedList.nodeCount());
        singleLinkedList.addByOrder(heroNode5);
        singleLinkedList.addByOrder(heroNode3);
        //heroNode3 = new HeroNode(3,"小吴","无用");
        //singleLinkedList.update(heroNode3);
        System.out.println("链表有效结点个数:"+singleLinkedList.nodeCount());
        //singleLinkedList.list();
        //HeroNode heroNode = singleLinkedList.getHeroNodeLastIndex(5);
        //heroNode.nickName="暴风雨";
        singleLinkedList.reverseLinkList();
        singleLinkedList.list();
        singleLinkedList.reversePrintByStack();
    }


    //无序添加
    public void add(HeroNode heroNode){
        HeroNode temp = headNode;
        while (true){
            if (temp.next==null) {break;}
            temp=temp.next;
        }
        temp.next=heroNode;
    }


    //有序添加
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = headNode;
        boolean flag=true;
        while (true){
            if (temp.next==null){break;}
            if (temp.next.no>heroNode.no) {
                break;
            } else if (temp.next.no==heroNode.no){
                flag=false;
                System.out.println("请勿重复添加"+heroNode.no+"号英雄");
            }
            temp=temp.next;
        }
        if (flag){
            heroNode.next=temp.next;
            temp.next=heroNode;
        }
    }

    //修改
    public void update(HeroNode newHeroNode){
        HeroNode temp = headNode;
        if (temp.next==null){
            System.out.println("链表为空~");
            return;
        }
        while (true){
            temp=temp.next;
            if (temp.no==newHeroNode.no){
                temp.name=newHeroNode.name;
                temp.nickName=newHeroNode.nickName;
                break;
            }
            if (temp==null){
                System.out.println("未找到编号为"+newHeroNode.no+"要修改的结点");
                break;
            }
        }
    }

    //删除 TODO
    public void delete(int heroNo){

    }

    //统计链表结点个数
    public int nodeCount(){
        int count=0;
        HeroNode temp = headNode;
        while (true){
            if (temp.next==null){
                return count;
            }
            temp=temp.next;
            count++;
        }
    }
    // 查找单链表的倒数第k个结点  新浪面试题
    public HeroNode getHeroNodeLastIndex(int k){
        if (k>nodeCount()){
            throw new RuntimeException("输入的数值不能大于链表长度");
        }else {
            HeroNode temp=headNode.next;
            for (int i=0;i<nodeCount()-k;i++){
                temp=temp.next;
            }
            return temp;
        }
    }
    //链表的反转  腾讯面试题
    public void reverseLinkList(){
        HeroNode reverseHead = new HeroNode(0,"","");
        while (true){
            HeroNode temp=headNode.next;
            if (temp==null){
                break;
            }
            headNode.next=temp.next;
            temp.next=reverseHead.next;
            reverseHead.next=temp;
        }
        headNode=reverseHead;
    }

    //链表逆序打印 百度面试题 , 很差的解法
    public void reversePrint(){
        reverseLinkList();
        list();
        reverseLinkList();
    }

    //方式2 将结点按顺序压入栈中
    public void reversePrintByStack(){
        if (headNode.next==null){return;}
        Stack<HeroNode> heroNodes = new Stack<>();
        HeroNode cur =headNode.next;
        while (cur!=null){
            heroNodes.add(cur);
            cur=cur.next;
        }
        while (heroNodes.size()>0){
            System.out.println(heroNodes.pop());
        }
    }

    //将两个有序列表合并,且合并过后列表仍然有序; TODO

    public void list(){
        if (headNode.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = headNode.next;
        while (true){
            System.out.println(temp);
            if (temp.next==null){
                break;
            }
            temp= temp.next;
        }
    }
}
class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Hero{ " +no +"—"+nickName+name+" }";
    }
}
